package com.wlcorp.forcazamis.web.action.baseinfo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.baseinfo.IFactoryService;
import com.wlcorp.forcazamis.domain.baseinfo.Factory;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class FactoryAction extends DispatchAction {
	public ActionForward getFactoryList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Factory> factoryList = ContextUtil.getInstance().getFactoryService().getFactoryList(null);
		request.setAttribute("factoryList",factoryList);
		return mapping.findForward("list");
	}
	
	public ActionForward getFactory(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Factory factory = new Factory();
		WebUtil.setRequestToObject(request,factory);
		factory = ContextUtil.getInstance().getFactoryService().getFactoryById(factory.getFactoryId());
		if(null == factory){
			factory = new Factory();
		}
		request.setAttribute("factory",factory);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyFactory(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Factory factory = new Factory();
		WebUtil.setRequestToObject(request,factory);
		ContextUtil.getInstance().getFactoryService().saveFactory(factory);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == factory.getStatus()){
			WebUtil.setAlertMessage(request,factory.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"操作成功！");
		}
		
		request.setAttribute("factory",factory);
		return mapping.findForward("modify");
	}
	
	public ActionForward deleteFactory(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Factory factory = new Factory();
		WebUtil.setRequestToObject(request,factory);
		IFactoryService service = ContextUtil.getInstance().getFactoryService();
		factory = service.getFactoryById(factory.getFactoryId());
		if(null != factory){
			service.deleteFactory(factory);
		}
		return getFactoryList(mapping,form,request,response);
	}
}
